package com.freak.wanandroid.model.classify;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.freak.httpmanager.log.LogUtil;
import com.freak.wanandroid.R;
import com.freak.wanandroid.base.fragment.BaseLifecycleFragment;
import com.freak.wanandroid.bean.ClassifyBean;
import com.freak.wanandroid.bean.ClassifyTitleBean;
import com.freak.wanandroid.view.viewpager.DepthPageTransformer;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目分类
 */
public class ClassifyFragment extends BaseLifecycleFragment<ClassifyViewModel> {
    private ViewPager2 mClassViewPager;

    private List<String> mList;

    private List<Fragment> mFragmentList;

    private TabLayout mSlidingTabLayout;

    private List<ClassifyTitleBean> mClassifyBeanList;

    private BaseAdapter mBaseAdapter;

//    /**
//     * 断网重连
//     *
//     * @param networkType
//     */
//    @Override
//    public void onNetConnected(NetworkType networkType) {
//        super.onNetConnected(networkType);
//        if (classifyBeanList.size()==0) {
//            mViewModel.getClassifyTitle();
//        }
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_classify;
    }

    @Override
    protected void initEventAndData(View view) {
        mList = new ArrayList<>();
        mClassifyBeanList = new ArrayList<>();
        mFragmentList = new ArrayList<>();
        mClassViewPager = view.findViewById(R.id.class_view_pager);
        mSlidingTabLayout = view.findViewById(R.id.sliding_tab_layout);
    }

    @Override
    protected void dataObserver() {
        mViewModel.mGetClassify.observe(this, this::getClassifySuccess);
        mViewModel.mGetClassifyError.observe(this, s -> {
            dismissLoading();
        });
        mViewModel.mGetClassifyTitle.observe(this, this::getClassifyTitleSuccess);
        mViewModel.mGetClassifyTitleError.observe(this, s -> {
            dismissLoading();
        });
    }

    @Override
    protected void onResumeLoadData() {
        mViewModel.getClassifyTitle();
        showLoading();
    }

    public void getClassifySuccess(ClassifyBean model) {
        dismissLoading();
    }

    public void getClassifyTitleSuccess(List<ClassifyTitleBean> model) {
        dismissLoading();
        mClassifyBeanList = model;
        for (ClassifyTitleBean classifyTitleBean : model) {
            mList.add(classifyTitleBean.getName());
        }
        mBaseAdapter = new BaseAdapter(mCurrentActivity, mList, mFragmentList);
        mClassViewPager.setAdapter(mBaseAdapter);
        mClassViewPager.setOffscreenPageLimit(1);
        new TabLayoutMediator(mSlidingTabLayout, mClassViewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                LogUtil.i("mList.get(position) " + mList.get(position));
                tab.setText(mList.get(position));
            }
        }).attach();
        mClassViewPager.setPageTransformer(new DepthPageTransformer());
    }

    class BaseAdapter extends FragmentStateAdapter {
        private List<String> mList;
        private List<Fragment> mFragmentList;

        public BaseAdapter(@NonNull FragmentActivity fragmentActivity, List<String> list, List<Fragment> fragmentList) {
            super(fragmentActivity);
            mList = list;
            mFragmentList = fragmentList;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return new DetailFragment(mClassifyBeanList.get(position).getId());
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }
}
